$(function(){
	/**
	 * 查询条件
	 */
	let params = "";
	/**
	 * 处理查询条件
	 */
	let searchForm = $(".form-search");
	searchForm.submit(function(){
		params = searchForm.serialize();
		loadData();
		return false;
	});
	
	let s=$(".sta").text();
	let status=function(){
		if(s=="审批中的文章"){
			return "/news-approval-search.html";
		}else{
			return "/news-reject-search.html";
		}
	};
	
	/**
	 * 加载分页数据
	 * @param {int} current
	 */
	let loadData = function(current=1){
		$.get(
			status(),
			params+"&current="+current,
			function(data){
				formatData(data.list);
				formatPage(data);
			},"json"
		);
		
	}
	
	/**
	 * 将数字状态转为中文表示
	 * @param int a
	 */
	let fn=function(a){
		if(a==0){
			return "审核中";
		}else if(a==1){
			return "未合格";
		}else{
			return "已发表";
		};
		
	}
	
	let datagrid = $("#tbd");
	let template  = datagrid.children().remove();
	/**
	 * 格式化数据
	 * @param {Object} list
	 */
	let formatData = function(list){
		datagrid.empty();
		$.each(list,function(i,item){
			let tr = template.clone().appendTo(datagrid);
			item.rowNumber=i+1;
			$.each(item, function(name,value) {
				tr.find("._"+name).text(value);
				if(name=="newsStatus"){
					tr.find(".newsStatus").text(fn(value));
				};
			});
			
			tr.find("a:eq(0)").attr("href","/news-show-"+item.newsId+".html");
			tr.find("a").attr("data-id",item.newsId);
			tr.find("a:eq(1)").attr("href","news-modify-"+item.newsId+".do");
			
		});
	}
	
	
	let pagination = $(".pagination");
	let liTemplate = pagination.children(":eq(1)").remove();
	/**
	 * 格式化页码
	 * @param {Object} p
	 */
	let formatPage = function(p){
		pagination.children(":not(:eq(0),:eq(-1))").remove();
		
		pagination.children(":last").attr("data-page",p.pages);
		
		$.each(p.navigatepageNums,function(index,i){
			let li = liTemplate.clone().insertBefore(pagination.children(":eq(-1)"));
			li.children("a").text(i);
			li.attr("data-page",i);
			if(p.pageNum==i){
				li.addClass("active");
			}
		});
	}

	/**
	 * 处理分页事件
	 */
	pagination.on("click","[data-page]",function(){
		let current = $(this).data("page");
		loadData(current);
	});
	
	loadData();
})